package com.kbbywyclient.ui.activity;

import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.data.entity.Event;
import com.kbbywyclient.data.entity.LoansTableMenu;
import com.kbbywyclient.data.entity.ResultInfo;
import com.kbbywyclient.data.entity.StatusUpdate;
import com.kbbywyclient.data.entity.TabledBuy;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.di.module.BoughtTableModule;
import com.kbbywyclient.presenter.BoughtTablePresenter;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.adapter.BoughtTableAdapter;
import com.kbbywyclient.ui.adapter.MenuProductAdapter;
import com.kbbywyclient.ui.adapter.MenuProfessionAdapter;
import com.kbbywyclient.ui.adapter.MenuPurposeAdapter;
import com.kbbywyclient.ui.adapter.MenuScreenStatusAdapter;
import com.kbbywyclient.ui.adapter.MenuStatusCreditCardAdapter;
import com.kbbywyclient.ui.contarct.BoughtTableContract;
import com.kbbywyclient.ui.fragment.StatusDialogFragment;
import com.kbbywyclient.util.ActivitySkipUtil;
import com.kbbywyclient.util.StringUtil;
import com.kbbywyclient.util.ViewUtils;
import com.kbbywyclient.util.pre.PreUtils;
import com.kbbywyclient.widget.CustomDialog;
import com.kbbywyclient.widget.CustomPopwin;
import com.kbbywyclient.widget.CustomToolBar;
import com.kbbywyclient.widget.GetMoreListView;
import com.kbbywyclient.widget.MyGridView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Description:
 * Created by FHW
 * on 2017/4/12
 */

public class BoughtTableActivity extends BaseActivity implements BoughtTableContract.View {

    @BindView(R.id.v_toolbar)
    CustomToolBar v_toolbar;
    @BindView(R.id.ptr_view)
    PtrClassicFrameLayout ptr_view;
    @BindView(R.id.lv_bought_table)
    GetMoreListView lv_bought_table;

    @BindView(R.id.ll_view)
    LinearLayout ll_view;
    @BindView(R.id.ll_toolbar)
    LinearLayout ll_toolbar;
    @BindView(R.id.tv_purpose)
    TextView tv_purpose;
    @BindView(R.id.tv_product)
    TextView tv_product;
    @BindView(R.id.tv_profession_info)
    TextView tv_profession_info;
    @BindView(R.id.tv_screen)
    TextView tv_screen;
    @BindView(R.id.iv_purpose)
    ImageView iv_purpose;
    @BindView(R.id.iv_product)
    ImageView iv_product;
    @BindView(R.id.iv_profession_info)
    ImageView iv_profession_info;
    @BindView(R.id.iv_screen)
    ImageView iv_screen;

    @Inject
    BoughtTablePresenter boughtTablePresenter;

    private String CompanyID = "";
    private String CompanyTypeID = "";
    private String loansType = "0";//贷款类型
    private String workTypeCode = "";//职业身份码表
    private String productLoansID = "0";//指定受理贷款产品ID
    private String keyWord = "";//搜索关键字
    private String status = "";//订单状态Code集 ,多选用逗号隔开
    private long applyStarDateTime = 0;//时间段开始，int时间戳
    private long applyEndDateTime = 0;//时间段结束，int时间戳

    private int mType = 0;

    private BoughtTableAdapter boughtTableAdapter = null;
    private boolean isFirstLoad = true;

    private CustomPopwin popwin;

    private List<LoansTableMenu.LoansTypeBean> LoansTypeList = null;//贷款用途
    private MenuPurposeAdapter menuPurposeAdapter = null;

    private List<LoansTableMenu.ProductListBean> productList = null;//贷款产品
    private MenuProductAdapter menuProductAdapter = null;

    private List<LoansTableMenu.WorkTypeListBean> workTypeList = null;
    private MenuProfessionAdapter menuProfessionAdapter = null;

    private List<LoansTableMenu.StatusBean> statusBeanList = null;//处理状态
    private MenuScreenStatusAdapter menuScreenStatusAdapter = null;

    private ListView lv_purpose;
    private ListView lv_product;
    private ListView lv_profession_info;
    private LinearLayout ll_screen;
    private EditText et_keyword;
    private MyGridView gv_status;
    private RadioGroup rg_apply_time;
    private RadioButton rb_one_month;
    private RadioButton rb_two_month;
    private RadioButton rb_three_month;
    private EditText et_start_time;
    private EditText et_end_time;

    private int rbType = -1;

    private String formatStr = "yyyy-MM-dd";

    private List<ResultInfo> resultInfoList = null;

    private int applyLoansID = -1;
    private String applyLoansSN = "";
    private int mPosition = 0;

    private CustomDialog dialog;
    private String statusName = "";

    private CustomPopwin popwinModify;
    private MenuStatusCreditCardAdapter menuStatusCreAdapter;

    private String successLoansTerm = "";//期限
    private String successLoansMoney = "";//金额
    private String monthlyInterestRate = "";//利率

    private LinearLayout ll_one;
    private LinearLayout ll_two;
    private EditText et_successLoansMoney;
    private EditText et_monthlyInterestRate;
    private EditText et_successLoansTerm;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_bought_table;
    }

    @OnClick({R.id.ll_purpose, R.id.ll_product, R.id.ll_profession_info, R.id.ll_screen})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_purpose://贷款用途
                mType = 0;
                showView(mType);
                showPop(view);
                if (!isFirstLoad && LoansTypeList.size() < 0) {
                    boughtTablePresenter.getloansOrdersMenu(PreUtils.getUser(this).getSalesID() + "");
                }
                break;
            case R.id.ll_product://贷款产品
                mType = 1;
                showView(mType);
                showPop(view);
                if (!isFirstLoad && productList.size() < 0) {
                    boughtTablePresenter.getloansOrdersMenu(PreUtils.getUser(this).getSalesID() + "");
                }
                break;
            case R.id.ll_profession_info://职业信息
                mType = 2;
                showView(mType);
                showPop(view);
                if (!isFirstLoad && workTypeList.size() < 0) {
                    boughtTablePresenter.getloansOrdersMenu(PreUtils.getUser(this).getSalesID() + "");
                }
                break;
            case R.id.ll_screen://持卡情况
                mType = 3;
                showView(mType);
                showPop(view);
//                if (!isFirstLoad && creditCardBeanList.size() < 0) {
//                    boughtTablePresenter.getloansOrdersMenu(PreUtils.getUser(this).getSalesID() + "");
//                }
                break;
        }
    }

    private void showPop(View view) {
//        View proView = LayoutInflater.from(this).inflate(R.layout.pop_bought_table, null, false);
//        lv_purpose = (ListView) proView.findViewById(R.id.lv_purpose);
//        lv_product = (ListView) proView.findViewById(R.id.lv_product);
//        lv_profession_info = (ListView) proView.findViewById(R.id.lv_profession_info);
//        ll_screen = (LinearLayout) proView.findViewById(R.id.ll_screen);
//
//        et_keyword = (EditText) proView.findViewById(R.id.et_keyword);
//        gv_status = (MyGridView) proView.findViewById(R.id.gv_status);
//        rg_apply_time = (RadioGroup) proView.findViewById(R.id.rg_apply_time);
//        rb_one_month = (RadioButton) proView.findViewById(R.id.rb_one_month);
//        rb_two_month = (RadioButton) proView.findViewById(R.id.rb_two_month);
//        rb_three_month = (RadioButton) proView.findViewById(R.id.rb_three_month);
//        et_start_time = (EditText) proView.findViewById(R.id.et_start_time);
//        et_end_time = (EditText) proView.findViewById(R.id.et_end_time);
//        TextView tv_reset = (TextView) proView.findViewById(R.id.tv_reset);
//        TextView tv_determine = (TextView) proView.findViewById(R.id.tv_determine);
//
//        View v_dimess = proView.findViewById(R.id.v_dimess);
//
//        LinearLayout ll_purpose = (LinearLayout) proView.findViewById(R.id.ll_purpose);
//        LinearLayout ll_product = (LinearLayout) proView.findViewById(R.id.ll_product);
//        LinearLayout ll_profession_info = (LinearLayout) proView.findViewById(R.id.ll_profession_info);
//        LinearLayout ll_screen_bt = (LinearLayout) proView.findViewById(R.id.ll_screen_bt);
//
//        setPopLvVisbilety();
//
//        //获取popupwindow高度确定动画开始位置
////        int contentHeight = PopUtils.getViewMeasuredHeight(proView);
//
//        if (popwin == null) {
//            popwin = new CustomPopwin(this, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
//        }
//        switch (mType) {
//            case 0://用途
////                popwin.setFocusable(false);
//                lv_purpose.setVisibility(View.VISIBLE);
//                if (LoansTypeList != null) {
//                    setPurposeAdapter(lv_purpose);
//                }
//                break;
//            case 1://产品
////                popwin.setFocusable(false);
//                lv_product.setVisibility(View.VISIBLE);
//                if (productList != null) {
//                    setProductAdapter(lv_product);
//                }
//                break;
//            case 2://职位
////                popwin.setFocusable(false);
//                lv_profession_info.setVisibility(View.VISIBLE);
//                if (workTypeList != null) {
//                    setProfessionAdapter(lv_profession_info);
//                }
//                break;
//            case 3://筛选
//                keyWord = "";
//                rbType = -1;
//                applyStarDateTime = 0;
//                applyEndDateTime = 0;
////                popwin.setFocusable(true);
//                ll_screen.setVisibility(View.VISIBLE);
//                if (statusBeanList != null) {
//                    setStatusAdapter(gv_status);
//                }
//                break;
//        }
//        popwin.setContentView(proView);
////        popwin.showAsDropDown(view, 0, 0);
//        popwin.showAtLocation(view, Gravity.NO_GRAVITY, 0, 0);
//        popwin.setFocusable(true);
//
//        popwin.setOnDismissListener(() -> {
//            setTextType();
//        });
//        lv_purpose.setOnItemClickListener((parent, view1, position, id) -> {
//            popwin.dismiss();
//            for (int i = 0; i < LoansTypeList.size(); i++) {
//                LoansTypeList.get(i).setCheck(false);
//                if (position == i) {
//                    LoansTypeList.get(i).setCheck(true);
//                    loansType = LoansTypeList.get(i).getID() + "";
//                    ViewUtils.setText(tv_purpose, LoansTypeList.get(i).getName());
//                }
//            }
//            ptr_view.autoRefresh();
//        });
//        lv_product.setOnItemClickListener((parent, view1, position, id) -> {
//            popwin.dismiss();
//            for (int i = 0; i < productList.size(); i++) {
//                productList.get(i).setCheck(false);
//                if (position == i) {
//                    productList.get(i).setCheck(true);
//                    productLoansID = productList.get(i).getID() + "";
//                    ViewUtils.setText(tv_product, productList.get(i).getName());
//                }
//            }
//            ptr_view.autoRefresh();
//        });
//        lv_profession_info.setOnItemClickListener((parent, view12, position, id) -> {
//            popwin.dismiss();
//            for (int i = 0; i < workTypeList.size(); i++) {
//                workTypeList.get(i).setCheck(false);
//                if (position == i) {
//                    workTypeList.get(i).setCheck(true);
//                    workTypeCode = workTypeList.get(i).getCode();
//                    ViewUtils.setText(tv_profession_info, workTypeList.get(i).getName());
//                }
//            }
//            ptr_view.autoRefresh();
//        });
//        //筛选
//        et_keyword.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                keyWord = s.toString();
//            }
//        });
//
//        rg_apply_time.setOnCheckedChangeListener((group, checkedId) -> {
//            rb_one_month.setBackgroundResource(R.drawable.shape_frame_line_dddddd);
//            rb_two_month.setBackgroundResource(R.drawable.shape_frame_line_dddddd);
//            rb_three_month.setBackgroundResource(R.drawable.shape_frame_line_dddddd);
//            rb_one_month.setTextColor(getResources().getColor(R.color.color_212121));
//            rb_two_month.setTextColor(getResources().getColor(R.color.color_212121));
//            rb_three_month.setTextColor(getResources().getColor(R.color.color_212121));
//            applyEndDateTime = DateUtils.getStringToDate(DateUtils.getAnyData(0, formatStr));
//            switch (checkedId) {
//                case R.id.rb_one_month:
//                    rbType = 0;
//                    rb_one_month.setBackgroundResource(R.drawable.textview_bg_14c095_6px);
//                    rb_one_month.setTextColor(getResources().getColor(R.color.color_ffffff));
//                    setClearCustomDate();
//                    applyStarDateTime = DateUtils.getStringToDate(DateUtils.getAnyData(30, formatStr));
//                    break;
//                case R.id.rb_two_month:
//                    rbType = 1;
//                    rb_two_month.setBackgroundResource(R.drawable.textview_bg_14c095_6px);
//                    rb_two_month.setTextColor(getResources().getColor(R.color.color_ffffff));
//                    setClearCustomDate();
//                    applyStarDateTime = DateUtils.getStringToDate(DateUtils.getAnyData(60, formatStr));
//                    break;
//                case R.id.rb_three_month:
//                    rbType = 2;
//                    rb_three_month.setBackgroundResource(R.drawable.textview_bg_14c095_6px);
//                    rb_three_month.setTextColor(getResources().getColor(R.color.color_ffffff));
//                    setClearCustomDate();
//                    applyStarDateTime = DateUtils.getStringToDate(DateUtils.getAnyData(90, formatStr));
//                    break;
//            }
//        });
//        et_start_time.setOnClickListener(onClickListener);
//        et_end_time.setOnClickListener(onClickListener);
//        tv_reset.setOnClickListener(onClickListener);
//        tv_determine.setOnClickListener(onClickListener);
//
//        ll_purpose.setOnClickListener(onClickListener);
//        ll_product.setOnClickListener(onClickListener);
//        ll_profession_info.setOnClickListener(onClickListener);
//        ll_screen_bt.setOnClickListener(onClickListener);
//
//        v_dimess.setOnClickListener(v -> popwin.dismiss());

        StatusDialogFragment statusDialogFragment = new StatusDialogFragment(mType, LoansTypeList, productList, workTypeList, statusBeanList
                , rbType, applyStarDateTime, applyEndDateTime);
        statusDialogFragment.show(getFragmentManager(), "StatusDialog");

    }

//    private void setPopLvVisbilety() {
//        lv_purpose.setVisibility(View.GONE);
//        lv_product.setVisibility(View.GONE);
//        lv_profession_info.setVisibility(View.GONE);
//        ll_screen.setVisibility(View.GONE);
//    }

//    private View.OnClickListener onClickListener = v -> {
//        switch (v.getId()) {
//            case R.id.et_start_time:
//                Calendar startC = Calendar.getInstance();
//                new DatePickerDialog(this, (view, year, month, dayOfMonth) -> {
//                    applyStarDateTime = DateUtils.getStringToDate(year + "-" + month + "-" + dayOfMonth);
//                    et_start_time.setText(year + "-" + (++month) + "-" + dayOfMonth);
//                    rbType = -1;
//                    rg_apply_time.clearCheck();
//                }, startC.get(Calendar.YEAR), startC.get(Calendar.MONTH), startC
//                        .get(Calendar.DAY_OF_MONTH)).show();
//                break;
//            case R.id.et_end_time:
//                Calendar endC = Calendar.getInstance();
//                new DatePickerDialog(this, (view, year, month, dayOfMonth) -> {
//                    applyEndDateTime = DateUtils.getStringToDate(year + "-" + month + "-" + dayOfMonth);
//                    et_end_time.setText(year + "-" + (++month) + "-" + dayOfMonth);
//                    rbType = -1;
//                    rg_apply_time.clearCheck();
//                }, endC.get(Calendar.YEAR), endC.get(Calendar.MONTH), endC
//                        .get(Calendar.DAY_OF_MONTH)).show();
//                break;
//            case R.id.tv_reset:
//                keyWord = "";
//                et_keyword.setText(keyWord);
//
//                for (LoansTableMenu.StatusBean statusBean : statusBeanList) {
//                    statusBean.setCheck(false);
//                }
//                menuScreenStatusAdapter.notifyDataSetChanged();
//
//                rbType = -1;
//                rg_apply_time.clearCheck();
//
//                applyStarDateTime = 0;
//                applyEndDateTime = 0;
//                setClearCustomDate();
//
//                break;
//            case R.id.tv_determine://确定
//                popwin.dismiss();
//                status = "";
//                for (LoansTableMenu.StatusBean statusBean : statusBeanList) {
//                    if (statusBean.isCheck()) {
//                        status += statusBean.getCode() + ",";
//                    }
//                }
//
//                ptr_view.autoRefresh();
//                break;
//            case R.id.ll_purpose:
//                popwin.dismiss();
//                mType = 0;
//                showView(mType);
//                showPop(v);
//                break;
//            case R.id.ll_product:
//                popwin.dismiss();
//                mType = 1;
//                showView(mType);
//                showPop(v);
//                break;
//            case R.id.ll_profession_info:
//                popwin.dismiss();
//                mType = 2;
//                showView(mType);
//                showPop(v);
//                break;
//            case R.id.ll_screen_bt:
//                popwin.dismiss();
//                mType = 3;
//                showView(mType);
//                showPop(v);
//                break;
//        }
//    };

//    private void setClearCustomDate() {
//        et_start_time.setText("");
//        et_end_time.setText("");
//    }

//    /**
//     * 设置贷款用途 菜单数据
//     *
//     * @param lv
//     */
//    private void setPurposeAdapter(ListView lv) {
//        menuPurposeAdapter = new MenuPurposeAdapter(this, R.layout.item_menu_purpose, LoansTypeList);
//        lv.setAdapter(menuPurposeAdapter);
//    }
//
//    /**
//     * 设置贷款产品 菜单数据
//     *
//     * @param lv
//     */
//    private void setProductAdapter(ListView lv) {
//        menuProductAdapter = new MenuProductAdapter(this, R.layout.item_menu_purpose, productList);
//        lv.setAdapter(menuProductAdapter);
//    }
//
//    private void setProfessionAdapter(ListView lv) {
//        menuProfessionAdapter = new MenuProfessionAdapter(this, R.layout.item_menu_purpose, workTypeList);
//        lv.setAdapter(menuProfessionAdapter);
//    }
//
//    private void setStatusAdapter(MyGridView gv) {
//        menuScreenStatusAdapter = new MenuScreenStatusAdapter(this, R.layout.item_menu_status, statusBeanList);
//        gv.setAdapter(menuScreenStatusAdapter);
//    }


    private void showView(int type) {
        setTextType();
        switch (type) {
            case 0:
                tv_purpose.setTextColor(getResources().getColor(R.color.color_14C095));
                iv_purpose.setBackgroundResource(R.mipmap.icon_table_up);
                break;
            case 1:
                tv_product.setTextColor(getResources().getColor(R.color.color_14C095));
                iv_product.setBackgroundResource(R.mipmap.icon_table_up);
                break;
            case 2:
                tv_profession_info.setTextColor(getResources().getColor(R.color.color_14C095));
                iv_profession_info.setBackgroundResource(R.mipmap.icon_table_up);
                break;
            case 3:
                tv_screen.setTextColor(getResources().getColor(R.color.color_14C095));
                iv_screen.setBackgroundResource(R.mipmap.icon_funnel_s);
                break;
        }
    }

    private void setTextType() {
        tv_purpose.setTextColor(getResources().getColor(R.color.color_212121));
        tv_product.setTextColor(getResources().getColor(R.color.color_212121));
        tv_profession_info.setTextColor(getResources().getColor(R.color.color_212121));
        tv_screen.setTextColor(getResources().getColor(R.color.color_212121));
        iv_purpose.setBackgroundResource(R.mipmap.icon_table_dwon);
        iv_product.setBackgroundResource(R.mipmap.icon_table_dwon);
        iv_profession_info.setBackgroundResource(R.mipmap.icon_table_dwon);
        iv_screen.setBackgroundResource(R.mipmap.icon_funnel_n);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setScreenName("贷款-已购买客户表");
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);

        resultInfoList = new ArrayList<>();

        LoansTypeList = new ArrayList<>();
        productList = new ArrayList<>();
        workTypeList = new ArrayList<>();
        statusBeanList = new ArrayList<>();

        CompanyID = getIntent().getStringExtra("CompanyID");
        CompanyTypeID = getIntent().getStringExtra("CompanyTypeID");

        v_toolbar.setLeftViewOnClickLinster(v -> finish());
        v_toolbar.setRightViewOnClickLinster(v -> {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("type", 2);
            ActivitySkipUtil.skipAnotherActivity(this, DiscountActivity.class, map);
        });

        //加载更多
        lv_bought_table.setOnGetMoreListener(() -> {
            refresh(false);
        });
        lv_bought_table.setOnItemClickListener((parent, view, position, id) -> {
            if (position < resultInfoList.size() && resultInfoList.size() > 0) {
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("ApplyLoansID", resultInfoList.get(position).getApplyLoansID());
                map.put("ApplyLoansSN", resultInfoList.get(position).getApplyLoansSN());
                map.put("position", position);
                ActivitySkipUtil.skipAnotherActivity(this, LoansTableDetailActivity.class, map);
            }
        });
        lv_bought_table.setOnSileListener(new GetMoreListView.OnSileListener() {
            @Override
            public void onSlideUp(int direction) {
                ll_view.setVisibility(View.GONE);
                ViewUtils.setViewAnim(direction, ll_toolbar);
            }

            @Override
            public void onSlideDwon(int direction) {
                ll_view.setVisibility(View.INVISIBLE);
                ViewUtils.setViewAnim(direction, ll_toolbar);
            }
        });

    }

    @Override
    public void initData() {
        ptr_view.setLastUpdateTimeRelateObject(this);
        ptr_view.setPtrHandler(new PtrHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                lv_bought_table.setHasMore();
                refresh(true);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }
        });
        // the following are default settings
        ptr_view.setResistance(1.7f);
        ptr_view.setRatioOfHeaderHeightToRefresh(1.2f);
        ptr_view.postDelayed(new Runnable() {
            @Override
            public void run() {
                ptr_view.autoRefresh();
            }
        }, 100);
    }

    private void refresh(boolean isClean) {
        boughtTablePresenter.getLoansBuyOrders(isClean, PreUtils.getUser(this).getSalesID() + "", loansType, workTypeCode, productLoansID, keyWord
                , status, applyStarDateTime, applyEndDateTime);
    }

    @Override
    public void initAppComponent(AppComponent appComponent) {
        appComponent.getBoughtTableComponent(new BoughtTableModule(this)).inject(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Event.BoughtMobile boughtMobile) {
        setScreenAction("通信", "贷款-已购买客户-拨打电话");
    }

    /**
     * 购买 回调
     *
     * @param notifyCustomerTable
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Event.NotifyCustomerTable notifyCustomerTable) {
        resultInfoList.get(notifyCustomerTable.getPosition()).setSalesCount(5);
        boughtTableAdapter.notifyDataSetChanged();
    }

    /**
     * 买断、半买断  回调
     *
     * @param loansTableBuy
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Event.LoansTableBuy loansTableBuy) {
        applyLoansID = resultInfoList.get(loansTableBuy.getPosition()).getApplyLoansID();
        applyLoansSN = resultInfoList.get(loansTableBuy.getPosition()).getApplyLoansSN();
        mPosition = loansTableBuy.getPosition();
        boughtTablePresenter.getApplyLoansBuy(PreUtils.getUser(BoughtTableActivity.this).getSalesID() + ""
                , applyLoansID + "", applyLoansSN, "1"
                , "1");
    }

    /**
     * 修改 回调
     *
     * @param creditCardModify
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Event.CreditCardModify creditCardModify) {

        applyLoansID = resultInfoList.get(creditCardModify.getPosition()).getApplyLoansID();
        applyLoansSN = resultInfoList.get(creditCardModify.getPosition()).getApplyLoansSN();
        mPosition = creditCardModify.getPosition();

        for (LoansTableMenu.StatusBean statusBean : statusBeanList) {
            statusBean.setCheck(false);
            if (resultInfoList.get(creditCardModify.getPosition()).getStatus().equals(statusBean.getName())) {
                statusBean.setCheck(true);
                status = statusBean.getCode();
                statusName = statusBean.getName();
            }
        }
        showPopModify();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Event.ShowTopView showTopView) {
        showView(showTopView.getmType());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Event.DialogDismess dialogDismess) {
        setTextType();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Event.RefreshList refreshList) {
        switch (refreshList.getmType()) {
            case 0:
                for (int i = 0; i < LoansTypeList.size(); i++) {
                    LoansTypeList.get(i).setCheck(false);
                    if (refreshList.getPosition() == i) {
                        LoansTypeList.get(i).setCheck(true);
                        loansType = LoansTypeList.get(i).getID() + "";
                        ViewUtils.setText(tv_purpose, LoansTypeList.get(i).getName());
                    }
                }
                break;
            case 1:
                for (int i = 0; i < productList.size(); i++) {
                    productList.get(i).setCheck(false);
                    if (refreshList.getPosition() == i) {
                        productList.get(i).setCheck(true);
                        productLoansID = productList.get(i).getID() + "";
                        ViewUtils.setText(tv_product, productList.get(i).getName());
                    }
                }
                break;
            case 2:
                for (int i = 0; i < workTypeList.size(); i++) {
                    workTypeList.get(i).setCheck(false);
                    if (refreshList.getPosition() == i) {
                        workTypeList.get(i).setCheck(true);
                        workTypeCode = workTypeList.get(i).getCode();
                        ViewUtils.setText(tv_profession_info, workTypeList.get(i).getName());
                    }
                }
                break;
            case 3:
                keyWord = refreshList.getKeyWord();
                status = refreshList.getStatus();
                applyStarDateTime = refreshList.getApplyStarDateTime();
                applyEndDateTime = refreshList.getApplyEndDateTime();
                rbType = refreshList.getRbType();
                break;
        }

        ptr_view.autoRefresh();

    }

    private void showPopModify() {

        View proView = LayoutInflater.from(this).inflate(R.layout.pop_status_modify, null, false);

        ll_one = (LinearLayout) proView.findViewById(R.id.ll_one);
        ll_two = (LinearLayout) proView.findViewById(R.id.ll_two);

        ImageView iv_colse = (ImageView) proView.findViewById(R.id.iv_colse);
        ListView lv_status = (ListView) proView.findViewById(R.id.lv_status);
        TextView tv_determine = (TextView) proView.findViewById(R.id.tv_determine);

        et_successLoansMoney = (EditText) proView.findViewById(R.id.et_successLoansMoney);
        et_monthlyInterestRate = (EditText) proView.findViewById(R.id.et_monthlyInterestRate);
        et_successLoansTerm = (EditText) proView.findViewById(R.id.et_successLoansTerm);

        TextView tv_cancel = (TextView) proView.findViewById(R.id.tv_cancel);
        TextView tv_determine_two = (TextView) proView.findViewById(R.id.tv_determine_two);

        if (popwinModify == null) {
            popwinModify = new CustomPopwin(this, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0.6f);
        }

        if (statusBeanList != null) {
            setPopStatusAdapter(lv_status);
        }

        popwinModify.setContentView(proView);
        popwinModify.showAtLocation(proView, Gravity.CENTER, 0, 0);
        popwinModify.setFocusable(true);

        iv_colse.setOnClickListener(onListtener);
        tv_determine.setOnClickListener(onListtener);

        tv_cancel.setOnClickListener(onListtener);
        tv_determine_two.setOnClickListener(onListtener);

        lv_status.setOnItemClickListener((parent, view, position, id) -> {
            for (int i = 0; i < statusBeanList.size(); i++) {
                statusBeanList.get(i).setCheck(false);
                if (position == i) {
                    statusBeanList.get(i).setCheck(true);
                    status = statusBeanList.get(i).getCode();
                    statusName = statusBeanList.get(i).getName();
                    menuStatusCreAdapter.notifyDataSetChanged();
                }
            }
        });

        et_successLoansMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                successLoansMoney = s.toString();
            }
        });
        et_monthlyInterestRate.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                monthlyInterestRate = s.toString();
            }
        });
        et_successLoansTerm.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                successLoansTerm = s.toString();
            }
        });

    }

    private void setPopStatusAdapter(ListView lv_status) {
        menuStatusCreAdapter = new MenuStatusCreditCardAdapter(this, R.layout.item_menu_status_credit, statusBeanList);
        lv_status.setAdapter(menuStatusCreAdapter);
    }

    private View.OnClickListener onListtener = v -> {
        switch (v.getId()) {
            case R.id.iv_colse:
                popwinModify.dismiss();
                break;
            case R.id.tv_determine:
                ll_one.setVisibility(View.GONE);
                ll_two.setVisibility(View.VISIBLE);
                break;
            case R.id.tv_cancel:
                popwinModify.dismiss();
                break;
            case R.id.tv_determine_two:
                popwinModify.dismiss();
                boughtTablePresenter.getApplyLoansUpdate(PreUtils.getUser(this).getSalesID() + ""
                        , applyLoansID + "", applyLoansSN, status, "", successLoansTerm, successLoansMoney, monthlyInterestRate);
                break;
        }
    };

    @Override
    public void showLoadingDialog(String s) {
        showProgress(s);
    }

    @Override
    public void closeLoadingDialog() {
        dismissProgress();
    }

    @Override
    public void loadSucBoughtTable(List<ResultInfo> resultInfoList) {
        ptr_view.refreshComplete();
        if (isFirstLoad) {
            ll_view.setVisibility(View.INVISIBLE);
            boughtTablePresenter.getloansOrdersMenu(PreUtils.getUser(this).getSalesID() + "");
        }
    }

    @Override
    public void loadFaiBoughtTable(String errorMsg) {
        ptr_view.refreshComplete();
        lv_bought_table.getMoreComplete();
    }

    @Override
    public void setAdapterOrNotify(List<ResultInfo> mResultInfoList) {

        if (resultInfoList.size() > 0) {
            resultInfoList.clear();
        }
        resultInfoList.addAll(mResultInfoList);

        if (boughtTableAdapter == null) {
            boughtTableAdapter = new BoughtTableAdapter(this, R.layout.item_bought_table, resultInfoList);
            lv_bought_table.setAdapter(boughtTableAdapter);
        } else {
            boughtTableAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void setNoMore() {
        lv_bought_table.setNoMore();
    }

    @Override
    public void setMoreComplete() {
        lv_bought_table.getMoreComplete();
    }

    @Override
    public void loadSucMenu(LoansTableMenu loansTableMenu) {

        LoansTypeList.addAll(loansTableMenu.getLoansType());
        LoansTableMenu.LoansTypeBean loansTypeBean = new LoansTableMenu.LoansTypeBean();
        loansTypeBean.setName("不限");
        loansTypeBean.setCheck(true);
        LoansTypeList.add(0, loansTypeBean);

        productList.addAll(loansTableMenu.getProductList());
        LoansTableMenu.ProductListBean productListBean = new LoansTableMenu.ProductListBean();
        productListBean.setName("不限");
        productListBean.setCheck(true);
        productList.add(0, productListBean);

        workTypeList.addAll(loansTableMenu.getWorkTypeList());
        LoansTableMenu.WorkTypeListBean workTypeListBean = new LoansTableMenu.WorkTypeListBean();
        workTypeListBean.setName("不限");
        workTypeListBean.setCheck(true);
        workTypeList.add(0, workTypeListBean);

        statusBeanList.addAll(loansTableMenu.getStatus());


        if (!isFirstLoad) {
            showPop(ll_toolbar);
        }
        isFirstLoad = false;

    }

    @Override
    public void loadFaiMenu(String errorMsg) {

    }

    @Override
    public void loadSucTableBuy(TabledBuy tabledBuy, String buyType) {
        if (StringUtil.isNotEmpty(buyType)) {//检验
            dialog = new CustomDialog(this, R.style.Customdialog, "提示", "是否确定购买？\n所需积分：" + tabledBuy.getScore() + " 积分"
                    , new CustomDialog.ButtonListener() {
                @Override
                public void left() {
                    dialog.dismiss();
                }

                @Override
                public void right() {
                    boughtTablePresenter.getApplyLoansBuy(PreUtils.getUser(BoughtTableActivity.this).getSalesID() + ""
                            , applyLoansID + "", applyLoansSN, "1"
                            , "");

                    dialog.dismiss();
                }
            }, getString(R.string.cancel), getString(R.string.determine));
            dialog.show();
        } else {//购买
            setScreenAction("购买", "贷款-已购买客户表-购买");
            resultInfoList.get(mPosition).setSalesCount(5);
            boughtTableAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void loadFaiTableBuy(String errorMsg) {
        if (errorMsg.equals("您的剩余积分不够,请充值!")) {
            dialog = new CustomDialog(this, R.style.Customdialog, "提示", errorMsg
                    , new CustomDialog.ButtonListener() {
                @Override
                public void left() {
                    dialog.dismiss();
                }

                @Override
                public void right() {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("CompanyID", CompanyID);
                    map.put("CompanyTypeID", CompanyTypeID);
                    ActivitySkipUtil.skipAnotherActivity(BoughtTableActivity.this, MineScoreActivity.class, map);
                    dialog.dismiss();
                }
            }, getString(R.string.cancel), getString(R.string.determine));
            dialog.show();
        }
    }

    @Override
    public void loadSucLoansUpdate(StatusUpdate statusUpdate) {
        resultInfoList.get(mPosition).setStatus(statusName);
//        resultInfoList.get(mPosition).setStatusName(statusName);
        boughtTableAdapter.notifyDataSetChanged();
    }

    @Override
    public void loadFaiLoansUpdate(String errorMsg) {

    }
}
